<?php
/**
 * 表单验证 / 表单模型
 * power by huanran.zhao
 * QQ: 523994553
 * 2013-01
 */
class FormApp extends CommonApp {

	/**
	 * 首页(模型分类)(分类FORM下)
	 */
	public function indexAct(){
		//读取模型授权分类列表
		$form_model = C('form_model');
		$result = '';
		if(is_array($form_model))
		foreach($form_model as $key=>$value){
			$result[$key]['cat_id'] = $key;
			$result[$key]['title'] = My::id2field('category',$key,'title');
		}
		$this->assign('result',$result);
		//print_r($result);
		$this->display();
	}
	
	/**
	 * 详细
	 */
	public function detailAct(){
		$cat_id = (int)My::arr2var($_GET,'cat_id');
		$current_cat = My::id2detail('category',$cat_id);
		$this->assign('current',$current_cat);
		//验证类型
		$getVerifyType = Form::getVerifyType();
		//ORM字段
		if(!empty($current_cat['params']['table'])){
			$temp_ = DB($current_cat['params']['table'])->getDbFields();
			if(is_array($temp_))
			foreach($temp_ as $value){
				$field[$value] = '';
			}
		}else{
			$field = '';
		}
		//表单验证项列表
		$vvv = DB('form')->where("cat_id='{$cat_id}' AND  is_custom=0")->order('ordering ASC')->select();
		if($field){
			//$vvv = My::customSort($vvv,$field,'field_name');//自定义对数组进行排序(数组,自定义顺序数组,字段)
			$this->assign('mainTable',true);
		}
		if(is_array($vvv))
		foreach($vvv as &$value){
			$field[$value['field_name']]=$value;
			//验证规则
			$value['verify_rule'] = My::decodeJson($value['verify_rule']);
			$str = '';
			if(is_array($value['verify_rule'])) foreach($value['verify_rule'] as $v) $str.=$getVerifyType[$v]['label'].'　';
			$field[$value['field_name']]['verify_rule'] = $str;
		}
		$this->assign('result',$field);
		//自字义表单的字段
		$form = DB('form')->where("`cat_id`='{$cat_id}' AND is_custom=1")->order('ordering ASC')->select();
		if(is_array($form))
		foreach($form as &$value){
			//验证规则
			$value['verify_rule'] = My::decodeJson($value['verify_rule']);
			$str = '';
			if(is_array($value['verify_rule'])) foreach($value['verify_rule'] as $v) $str.=$getVerifyType[$v]['label'].'　';
			$value['verify_rule'] = $str;
			//params
			$value['params'] = My::decodeJson($value['params']);
			//表单代码
			$value['code'] = "{{form name='params['{$value['field_name']}']' data=\$result.params[{$value['field_name']}] type={$value['widget_type']} formParams=\$FORM[{$value['field_name']}]['params']}}<br>
							  {{if strpos({$value['verify_rule']},'required')!==false}}<span class='red'>*</span>{{/if}}<br>
							  {{if \$FORM[{$value['field_name']}]['description']!=''}}<span class='doubt tip' title='{{\$FORM[{$value['field_name']}]['description']}}'></span>{{/if}}
							  ";
		}
		$this->assign('form',$form);
		//控件类型
		$this->assign('widget_type', Form::getType());
		$this->display();
	}
	
	/**
	 * 编辑/添加 页面
	 * @params int $_GET['id']
	 */
	public function modifyAct(){
		//当前页面数据
		$tr_html = Form::verify_detail_tr(array('id'=>I('get.id'),'field_name'=>I('get.field_name'),'cat_id'=>I('get.cat_id')));//{{*id值为verify表对应的id, id 和 field_name/cat_id两组参数二选一,Id优先级高*}}
		$this->assign('tr_html',$tr_html);
		$this->display();
	}

	/**
	 * 复制数据
	 */
	public function copyAct(){
		$id = My::arr2var($_POST,'id');
		//post表单提交
		if(My::is_id($id)){
			$cat_id = My::arr2var($_POST,'cat_id');
			if(!My::is_id($cat_id))
				$this->ajaxReturn(300,'cat_id错误!');
			$data = DB('form')->where("id={$id}")->find();
			if(!$data)
				$this->ajaxReturn(300,'找不到表单模型数据!');
			//判断是否有重复
			if(DB('form')->where("`cat_id`={$cat_id} AND `field_name`='{$data['field_name']}'")->count()>0)
				$this->ajaxReturn(300,'目标表单模型已存在该数据!');
			$data['cat_id'] = $cat_id;//目标表单模型分类
			$data['publish'] = 0;//新复制的内容,不发布
			unset($data['id']);//新增的数据不要id
			$ok = DB('form')->clear()->add($data);
			if($ok)
				$this->ajaxReturn(200,'复制成功!');
			else
				$this->ajaxReturn(300,'复制失败!');
			exit();
		}

		//显示表单
		$id = My::arr2var($_GET,'id');//当前表单模型 id
		$cat_id = My::id2field('form',$id,'cat_id');
		//表单模型授权 分类列表
		$form_model = C('form_model');
		if(!is_array($form_model))
			$this->ajaxReturn(404,'表单模型授权分类列表不存在!');
		$list = array();
		foreach($form_model as $key=>$value){
			$temp = My::id2field('category',$key,'title');
			if($temp!='')
				$list[$key] = $temp;
		}
		if(isset($list[$cat_id]))
			unset($list[$cat_id]);
		if(!$list)
			$this->ajaxReturn(404,'没有可复制到的其它表单模型!');
		$this->assign('list',$list);
		//当前数据标题
		$this->assign('model_title',My::id2field('form',$id,'title'));
		$this->display();
	}
	
	/**
	 * 保存表单验证
	 */
	function saveAct(){
		//echo "<pre>";print_r($_POST);exit;
		//处理保存
		if(isset($_POST['id']) AND isset($_POST['param']) AND is_array($_POST['param'])){
			$data['id'] = (int)My::arr2var($_POST,'id');
			$data['title'] = isset($_POST['param']['title'])?$_POST['param']['title']:'';
			$data['widget_type'] = isset($_POST['param']['widget_type'])?$_POST['param']['widget_type']:'';
			$data['verify_rule'] = (isset($_POST['param']['verify_rule']) AND is_array($_POST['param']['verify_rule']))?$_POST['param']['verify_rule']:'';
			$data['publish'] = isset($_POST['param']['publish'])?$_POST['param']['publish']:0;
			$data['description'] = isset($_POST['param']['description'])?$_POST['param']['description']:'';
			$data['field_name'] = isset($_POST['param']['field_name'])?$_POST['param']['field_name']:'';
			$data['cat_id'] = (int)My::arr2var($_POST,'cat_id');
			$data['params'] = My::arr2var($_POST,'param.params');
		}
		if(trim($_POST['param']['title'])=='')
			$this->ajaxReturn(300,'标题不能为空!');
		if(empty($_POST['param']['widget_type']))
			$this->ajaxReturn(300,'控件类型不能为空!');
		if(My::arr2var($_POST,'is_custom')==1) $data['is_custom']=1;//是否自定义字段
		//判断重复(没有id值 AND 同字段名)
		if(!$data['id'] AND DB('form')->where("cat_id='{$data['cat_id']}' AND field_name='{$data['field_name']}'")->count()>0)
			$this->ajaxReturn(300,'字段名不能重复');
		//update or insert
		if(My::is_id($data['id']))
			$ok = DB('form')->clear()->save($data);
		else
			$ok = DB('form')->clear()->add($data);
		//清除表单验证缓存文件
		$cacheFile = DATA_DIR.'Cache/verify_'.M('category')->id2item($data['cat_id']).'.php';
		if(file_exists($cacheFile)) unlink($cacheFile);
		$this->ajaxReturn(200,'操作成功');
	}

	/**
	 * 删除单个数据
	 */
	function deleteAct(){
		$id = My::arr2var($_GET,'id');
		if(!My::is_id($id))
			$this->ajaxReturn(300,'ID有误!');
		if(DB('form')->where("id={$id}")->count()==0)
			$this->ajaxReturn(300,'数据不存在!');
		M('recycle')->set('form',$id);
		$ok = DB('form')->where("id={$id}")->clear()->delete();
		if($ok)
			$this->ajaxReturn(200,'删除成功!');
		else
			$this->ajaxReturn(300,'删除失败!');
	}
		
		
		
		
		

	/**
	 * ajax
	 * 表单验证设置项,得到tr一行的html
	 * @param string $_POST['field_name'] 字段名
	 * @param int $_POST['cat_id'] 分类id(表单验证单元)
	 */
	function verify_detail_tr_ajaxAct(){
		$param['field_name'] = My::arr2var($_POST,'field_name');
		$param['cat_id'] = My::arr2var($_POST,'cat_id');
		if(!My::is_var($param['field_name']) OR !My::is_id($param['cat_id']))	return false;//字段名不合法 或 分类id非数字
		echo Form::verify_detail_tr($param);
	}
	
	/**
	 * ajax
	 * 表单模型参数设置->控件类型onchange时->得到相应的控件相关附加参数
	 */
	public function widget_type_change_ajaxAct(){
		$type = My::arr2var($_POST,'type');
		$id = My::arr2var($_POST,'id');
		if(!My::is_var($type)) return false;
		echo Form::widget_type_change($type,$id);
	}
	
	/**
	 * ajax
	 * 数据库名称->相应的字段
	 */
	public function dbname2fieldsAct(){
		$params['type'] = 'select_db_fields';
		$params['table'] = My::arr2var($_POST,'table');
		$params['name'] = My::arr2var($_POST,'name');
		$params['id'] = My::arr2var($_POST,'id');
		echo Form::select_db($params);
	}

}